<?php
namespace app\webscoket\handler;

use app\common\model\member\MemberModel;
use app\common\service\member\MemberAccountService;
use app\common\service\order\ShopOrderService;
use app\common\service\webscoket\CacheRedisService;
use app\webscoket\Manager;
use app\webscoket\Response;
use app\webscoket\Room;
use Swoole\Server;
use think\facade\Db;
use think\facade\Log;

/**
 * Class UserHandler
 * @package app\webscoket\handler
 */
class UserHandler
{
    /**
     * @var Manager
     */
    protected $manager;

    /**
     * @var Room
     */
    protected $room;

    /**
     * @var int
     */
    protected $fd;

    protected $formType;
    public function handle($event)
    {
        [$method, $result, $manager, $room] = $event;
        $this->manager = $manager;
        $this->room = $room;
        $this->fd = array_shift($result);
        $this->formType = array_shift($result);
        if (method_exists($this, $method)) {
            return $this->{$method}(...$result);
        } else {
            Log::error('socket 回调事件' . $method . '不存在,消息内容为:' . json_encode($result));
            return null;
        }
    }
    /**
     * 关闭连接触发
     * @param array $data
     * @param Response $response
     */
    public function close(array $data, Response $response)
    {
        if(isset($data['user_id'])){
            $member_model = new MemberModel();
            $res =$member_model->where($member_model->getPk(),  $data['user_id'])->update(['is_order_taking' => 0]);
        }
        return $response->success('close', ['msg'=>'关闭成功']);
    }

    /**
     * @param array $data
     * @return mixed
     * @author xaboy
     * @day 2020-05-06
     */
    public function ceshi(array $data, Response $response)
    {
        Log::info("我看看：".$this->fd);
        return $response->success('ceshi', ['msg'=>'收到了']);
    }

    /**
     * 开启接单
     * @Author: 易军辉
     * @DateTime:2024-08-19 15:40
     * @param array $data
     * @param Response $response
     * @return bool|\think\response\Json|null
     */
    public function openReceivingOrder(array $data, Response $response)
    {
        return $response->success('openReceivingOrder', ['msg'=>'收到了']);
    }

    /**
     * 获取是否有订单
     * @Author: 易军辉
     * @DateTime:2024-08-29 11:26
     * @param array $data
     * @param Response $response
     */
    public function getOrder(array $data, Response $response)
    {
        if(!isset($data['user_id'])){
            return $response->success('ping', ['now' => time()]);
        }
        //开启接单
        if($this->formType == 'onOpen') {
            $member_model = new MemberModel();
            $res =$member_model->where($member_model->getPk(),  $data['user_id'])->update(['is_order_taking' => 1]);
        }
        //检查是否有待接订单
        $list = ShopOrderService::getOrderByMember($data['user_id']);
        if($list){
            return $response->success('getOrder',$list);
        }
        //第一次建立链接
        if($this->formType == 'onOpen' && count($list)<=0){
            //暂无订单，通过买家ID去查找可接订单
            $order_list = MemberAccountService::getOrderByAccount($data['user_id'],null,1);
            if($order_list){
                $list = ShopOrderService::getOrderByMember($data['user_id']);
                if($list){
                    return $response->success('getOrder',$list);
                }
            }
        }
//        else if(CacheRedisService::get('OrderAssignListener_'.$data['user_id'])){
//            $list = ShopOrderService::getOrderByMember($data['user_id']);
//            if($list){
//                return $response->success('getOrder',$list);
//            }
//        }
        return $response->success('ping', ['now' => time()]);
    }

}
